package com.ichina.chat.dao.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ichina.chat.dao.ReplyDao;
import com.ichina.chat.util.Page;
import com.ichina.chat.vo.Reply;

@Component
@Transactional
public class ReplyDaoimpl implements ReplyDao {

	@Resource
	SessionFactory sessionFactory;
	private Session session = null;
	private Query query = null;
	private int count = 0;
	private String hql = "";

	public void addReply(Reply r) throws Exception {

		sessionFactory.getCurrentSession().save(r);
	}

	public void deleteReplyBySid(int sid) throws Exception {

		sessionFactory.getCurrentSession().delete(
				sessionFactory.getCurrentSession().load(Reply.class, sid));

	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Reply> queryReplyAll(Page page) throws Exception {
		hql = "from Reply";
		query = session.createQuery(hql);
		query.setFirstResult(page.getPagecode() * page.getPagesize()
				- page.getPagesize());
		query.setMaxResults(page.getPagesize());
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Reply> queryReplyBySid(int sid, Page page) throws Exception {
		hql = "from Reply where sid=" + sid;
		query = session.createQuery(hql);
		query.setFirstResult(page.getPagecode() * page.getPagesize()
				- page.getPagesize());
		query.setMaxResults(page.getPagesize());
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Reply> queryReplyByTime(Date time, Page page) throws Exception {
		hql = "from Reply where time=" + time;
		query = session.createQuery(hql);
		query.setFirstResult(page.getPagecode() * page.getPagesize()
				- page.getPagesize());
		query.setMaxResults(page.getPagesize());
		return query.list();
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public int queryReplyAll() throws Exception {
		hql = "from Reply ";
		count = session.createQuery(hql).list().size();
		return count;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public int queryReplyCountBySid(int sid) throws Exception {
		hql = "from Reply where sid=" + sid;
		count = session.createQuery(hql).list().size();
		return count;
	}

	public List<Reply> queryReplyRecordByName(String user) throws Exception {
		hql="from Reply";
		session = sessionFactory.getCurrentSession();
		query = session.createQuery(hql);
		//query.setString(0,user);
		List li = query.list();
		return li;
	}

	public List<Reply> queryReplyRecordByName_date(String user, String date)
			throws Exception {
		hql="from Reply where DATE_FORMAT(time,'%Y-%m-%d')=? and user =?";
		session=sessionFactory.getCurrentSession();
		query=session.createQuery(hql);
		query.setString(0,date);
		query.setString(1,user);
		return query.list();	
	}

}
